package com.example;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;

public class MyCustomRecipe extends Recipe {
    @Option(displayName = "Old String",
            description = "Old String.",
            example = "oldString")
    String oldString;

    @Option(displayName = "New String",
            description = "New String.",
            example = "newString")
    String newString;

    public MyCustomRecipe(@JsonProperty("oldString") String oldString, @JsonProperty("newString") String newString) {
        this.oldString = oldString;
        this.newString = newString;
    }

    @Override
    public String getDisplayName() {
        return "Custom string replacement";
    }

    @Override
    public String getDescription() {
        return "Replace all occurrences od 'oldString' with 'newString'";
    }

    public String getOldString() {
        return oldString;
    }

    public void setOldString(String old) {
        this.oldString = old;
    }

    public String getNewString() {
        return newString;
    }

    public void setNewString(String newStr) {
        this.newString = newStr;
    }

    @Override
    public TreeVisitor<?, ExecutionContext> getVisitor() {
        return new JavaIsoVisitor<ExecutionContext>() {
            @Override
            public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
                System.out.print("[visitLiteral]\n");
                J.Literal lit = super.visitLiteral(literal, ctx);
                if (literal.getValue() instanceof String) {
                    System.out.print("[visitLiteral] get string\n");
                    String value = (String) literal.getValue();
                    String newValue = value.replace(oldString, newString);
                    if (!value.equals(newValue)) {
                        System.out.print("[visitLiteral] replace string, " + "old value: " + value + ", new value: " + newValue + "\n");
                        return lit
                                .withValue(newValue)
                                .withValueSource('"' + newString + '"');
                    }
                }
//                return super.visitLiteral(literal, ctx);
                return lit;
            }
        };
    }

//    @Override
//    public TreeVisitor<?, ExecutionContext> getVisitor() {
//        return new JavaIsoVisitor<ExecutionContext>() {
//            @Override
//            public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
//                J.Literal lit = super.visitLiteral(literal, ctx);
//                if (lit.getType() == JavaType.Primitive.String &&
//                        oldString.equals(lit.getValue())) {
//                    return lit
//                            .withValue(newString)
//                            .withValueSource('"' + newString + '"');
//                }
//                return lit;
//            }
//        };
//    }
}
